/*
 * Copyright (c) 2011, 2012 Alexander Kluth
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 */
package hardyhttp;

import hardyhttp.filesystem.Logger;
import hardyhttp.server.*;
import hardyhttp.config.*;
import hardyhttp.ressources.*;

/**
 * Here everything begins :)
 * 
 * @author Alexander Kluth
 */
public class Hardyhttp 
{
    /**
     * Okay, HERE everything begins :) 
     * We create a new thread where a new instance of Httpd is running
     * and setting up basic logging (logging will be configured later
     * by ServerConfig).
     * 
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        Httpd http;
        Thread th;
        
        // Before reading the configuration we will output everything 
        // to stdout
        Logger logger = new Logger(Logger.LOG_STDOUT);
        ServerConfig config = new ServerConfig(logger);
        
        System.out.println(Constants.program_name + " " + Constants.version);
        System.out.println(Constants.copyright + " - " + Constants.license);
        
        logger.logEvent("[INIT]   Starting configuration...");
        
        // Start initial configuration
        config.configure();
        
        if (config.loglevel == Logger.LOG_FILE)
        {
            System.out.println("! HINT: All following output is written to a logfile under logs/.");
        }
        
        logger.logEvent("[CONFIG] Configuration done.");
   
        logger.logEvent("[INIT]   Creating new instance of httpd...");
        http = new Httpd(config, logger);
        logger.logEvent("[INIT]   Successfully created new instance of httpd.");
        
        logger.logEvent("[INIT]   Creating new thread and spawning httpd ...");
        th = new Thread(http);
        
        //FIXME: This does not work as expected...
        if (config.daemon == true)
        {
            logger.logEvent("[INIT]   Configured as daemon, set daemon-flag");
            th.setDaemon(true);
        }
        
        // Start our httpd-thread
        th.start();
        logger.logEvent("[INIT]   Thread created, httpd spawned.");
        
        //FIXME: This does not work as expected either...
        if (config.daemon == true)
            logger.logEvent("[INIT]   Daemon started.");
    }
}